package org.zn.mvcbase.config;



import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.util.StringUtils;
import org.zn.mvcbase.config.property.HandlerProperties;
import org.zn.mvcbase.config.property.MybatisExtraProperties;

import java.util.List;





@org.springframework.context.annotation.Configuration
@ConditionalOnClass({MybatisAutoConfiguration.class})
@AutoConfigureBefore({MybatisAutoConfiguration.class})
@EnableConfigurationProperties(MybatisExtraProperties.class)
public class MybaitsAutoConfiguration implements ConfigurationCustomizer{
	
	@Autowired
	private MybatisExtraProperties properties;
	
	@Override
	public void customize(Configuration configuration) {
		
		//构建默认转换器
			
		//获取TypeHandler
		List<HandlerProperties> handlers = this.properties.getTypehandler();
		for (HandlerProperties typehandlerProperties : handlers) {
			try {
				TypeHandler<?> typehandler = typehandlerProperties.getClazz().newInstance();
				//注册类型转换执行器
				configuration.getTypeHandlerRegistry().register(typehandler);
				if(StringUtils.isEmpty(typehandlerProperties.getAlias())){
					continue;
				}
				//注册别名
				configuration.getTypeAliasRegistry().registerAlias(typehandlerProperties.getAlias(), typehandlerProperties.getClazz());
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			System.err.println(typehandlerProperties.getAlias());
			System.err.println(typehandlerProperties.getClazz());
		}
		System.err.println("配置类");
	}
	
	
	
	

	
	
	
	

}
